﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace XW.Utils
{
    public static class MemoryCacheServiceCollectionExtensions
    {
        /// <summary>
        /// 添加空缓存
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddDistributedNullCache(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            } 
            services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, NullDistributedCache>());

            return services;
        }
    }
    /// <summary>
    /// 空缓存
    /// </summary>
    public class NullDistributedCache : IDistributedCache
    {
        private static readonly Task CompletedTask = Task.FromResult<object>(null);
        public byte[] Get(string key)
        {
            return null;
        }

        public Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
        {
            return Task.FromResult<byte[]>(null); ;
        }

        public void Refresh(string key)
        {
          
        }

        public Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
        {
            return CompletedTask;
        }

        public void Remove(string key)
        {
            
        }

        public Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
        {
            return CompletedTask;
        }

        public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
        {
         
        }

        public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
        {
            return CompletedTask;
        }
    }
}
